package com.meryl.dtk;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.impl.file.PsiDirectoryFactory;
import com.meryl.dtk.utils.StringUtil;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author : Meryl
 * @Description:
 * @Date: Created in 2024/9/7 0:35
 */
public class StringToCamelAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        Project project = e.getProject();

        Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
        if (editor != null) {
            SelectionModel selectionModel = editor.getSelectionModel();
            int start = selectionModel.getSelectionStart();
            int end = selectionModel.getSelectionEnd();

            String selectedText = selectionModel.getSelectedText();
            String target = StringUtil.toCamel(selectedText);


            WriteCommandAction.runWriteCommandAction(project, () -> {
                try {
                    if(selectedText.contains("_")){
                        Document document = editor.getDocument();
                        document.replaceString(start, end, target);
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
        }

    }
}
